package com.handler;

import com.ymx.darling.bean.entity.BeanInfo;
import com.ymx.darling.bean.entity.BeanInfoManager;
import com.ymx.darling.bean.sql.SqlStatement;
import com.ymx.darling.build.mapper.MapperMethodInfoInfoManager;
import com.ymx.darling.build.mapper.method.ProviderMethodInfo;
import com.ymx.darling.session.Session;
import com.ymx.darling.wrapper.QueryWrapper;
import com.ymx.darling.wrapper.build.SelectBuilder;

/**
 * @author 爱Java的小于
 */
public class FindByQuery extends ProviderMethodInfo {
    @Override
    public Object invoke(Class<?> mapperClass, Session session, String sqlStatementId, Object[] param) throws Throwable {
        // 根据当前的Mapper获取表名
        Class<?> entityType = MapperMethodInfoInfoManager.getEntityType(mapperClass);
        BeanInfo beanInfo = BeanInfoManager.getBeanInfo(entityType);
        // 转换参数类型
        QueryWrapper queryWrapper = (QueryWrapper) param[0];
        // 设置自己的参数
        queryWrapper.limit(5); // 自己设置分页参数
        // 转换为sql
        SqlStatement select = new SelectBuilder().buildSelect(queryWrapper, beanInfo);

        return session.selectList(entityType, select.getSql(), select.getParamList().toArray());
    }
}
